{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2abef1e8",
   "metadata": {},
   "source": [
    "# NumPy的求和\n",
    "## 求和\n",
    "求和与加法的区别是什么？\n",
    "\n",
    "加法是在两个参数之间进行的，而求和是在n个元素之间进行的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1b0ecc23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 4 6]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr1 = np.array([1, 2, 3])\n",
    "arr2 = np.array([1, 2, 3])\n",
    "\n",
    "newarr = np.add(arr1, arr2)\n",
    "\n",
    "print(newarr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "84a3a093",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.array([1, 2, 3])\n",
    "arr2 = np.array([1, 2, 3])\n",
    "\n",
    "newarr = np.sum([arr1, arr2])\n",
    "\n",
    "print(newarr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58b5ad07",
   "metadata": {},
   "source": [
    "## 在一个轴上求和\n",
    "如果你指定axis=1，NumPy将对每个数组中的数字求和。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b7a9ce6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6 6]\n"
     ]
    }
   ],
   "source": [
    "newarr = np.sum([arr1, arr2], axis=1)\n",
    "\n",
    "print(newarr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86e55728",
   "metadata": {},
   "source": [
    "## 累加\n",
    "累加是指将数组中的元素部分相加。\n",
    "\n",
    "例如，[1, 2, 3, 4]的部分相加是[1, 1+2, 1+2+3, 1+2+3+4] = [1, 3, 6, 10]。\n",
    "\n",
    "用cumsum()函数执行部分求和。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2637ca77",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 3 6]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([1, 2, 3])\n",
    "\n",
    "newarr = np.cumsum(arr)\n",
    "\n",
    "print(newarr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17139dee",
   "metadata": {},
   "source": [
    "# NumPy乘积\n",
    "## 乘积\n",
    "要找到一个数组中各元素的乘积，可以使用prod()函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b0694d29",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "24\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([1, 2, 3, 4])\n",
    "\n",
    "x = np.prod(arr)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "626db04b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "40320\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.array([1, 2, 3, 4])\n",
    "arr2 = np.array([5, 6, 7, 8])\n",
    "\n",
    "x = np.prod([arr1, arr2])\n",
    "\n",
    "print(x)\n",
    "# 1*2*3*4*5*6*7*8 = 40320"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d53d847b",
   "metadata": {},
   "source": [
    "## 在一个轴上的乘积\n",
    "如果你指定axis=1，NumPy将返回每个数组的乘积。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a6ec41a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  24 1680]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.array([1, 2, 3, 4])\n",
    "arr2 = np.array([5, 6, 7, 8])\n",
    "\n",
    "newarr = np.prod([arr1, arr2], axis=1)\n",
    "\n",
    "print(newarr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ab51166",
   "metadata": {},
   "source": [
    "## 累乘\n",
    "累乘是指部分地取积。\n",
    "\n",
    "例如：[1, 2, 3, 4]的部分积是[1, 1 * 2, 1 * 2 * 3, 1 * 2 * 3 * 4] = [1, 2, 6, 24]\n",
    "\n",
    "用cumprod()函数完成部分求和。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "17b6de1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[   5   30  210 1680]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([5, 6, 7, 8])\n",
    "\n",
    "newarr = np.cumprod(arr)\n",
    "\n",
    "print(newarr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "911c15b4",
   "metadata": {},
   "source": [
    "# NumPy的差分\n",
    "## 差分\n",
    "一个离散的差意味着减去两个连续的元素。\n",
    "\n",
    "例如，对于[1, 2, 3, 4]，离散差值是[2-1, 3-2, 4-3] = [1, 1, 1] 。\n",
    "\n",
    "要找到离散差值，请使用diff()函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cdced225",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  5  10 -20]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([10, 15, 25, 5])\n",
    "\n",
    "newarr = np.diff(arr)\n",
    "\n",
    "print(newarr)\n",
    "# 15-10=5, 25-15=10, and 5-25=-20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c4fe68db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  5 -30]\n"
     ]
    }
   ],
   "source": [
    "newarr = np.diff(arr, n=2)\n",
    "\n",
    "print(newarr)\n",
    "# 15-10=5, 25-15=10, and 5-25=-20\n",
    "# 10-5=5 and -20-10=-30"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0f7ab9e",
   "metadata": {},
   "source": [
    "# NumPy LCM 最小公倍数\n",
    "## 寻找LCM(最小公倍数)\n",
    "最小公倍数是指两个数字的公倍数，即最小的数字。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5ac8f6d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n"
     ]
    }
   ],
   "source": [
    "num1 = 4\n",
    "num2 = 6\n",
    "\n",
    "x = np.lcm(num1, num2)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e8a18e9",
   "metadata": {},
   "source": [
    "## 寻找数组中的最小公倍数\n",
    "要找到一个数组中所有数值的最小公倍数，你可以使用reduce()方法。\n",
    "\n",
    "reduce()方法将对每个元素使用ufunc，在这里是lcm()函数，并将数组减少一维。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "04481339",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([3, 6, 9])\n",
    "\n",
    "x = np.lcm.reduce(arr)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "bae3a2ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  2  3  4  5  6  7  8  9 10]\n",
      "2520\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(1, 11)\n",
    "\n",
    "x = np.lcm.reduce(arr)\n",
    "\n",
    "print(arr)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7412faf1",
   "metadata": {},
   "source": [
    "# NumPy GCD 最大公约数\n",
    "## 寻找GCD（最大公约数）\n",
    "GCD（最大公约数），也被称为HCF（最高公因数），是两个数字的最大公因数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "94543682",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "num1 = 6\n",
    "num2 = 9\n",
    "\n",
    "x = np.gcd(num1, num2)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "562db29f",
   "metadata": {},
   "source": [
    "## 寻找数组中的GCD\n",
    "要找到一个数组中所有数值的最高公因子，你可以使用reduce()方法。\n",
    "\n",
    "reduce()方法将对每个元素使用ufunc，在这里是gcd()函数，并将数组减少一维。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2f802573",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([20, 8, 32, 36, 16])\n",
    "\n",
    "x = np.gcd.reduce(arr)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40bf548e",
   "metadata": {},
   "source": [
    "# NumPy三角函数\n",
    "## 三角函数\n",
    "NumPy提供了sin()、cos()和tan()三个函数，它们以弧度为单位取值并产生相应的sin、cos和tan值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8c16399f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n"
     ]
    }
   ],
   "source": [
    "x = np.sin(np.pi/2)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4ae49ae4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.         0.8660254  0.70710678 0.58778525]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([np.pi/2, np.pi/3, np.pi/4, np.pi/5])\n",
    "\n",
    "x = np.sin(arr)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32fe53c0",
   "metadata": {},
   "source": [
    "## 角度转换为弧度\n",
    "默认情况下，所有的三角函数都以弧度为参数，但我们可以在NumPy中把弧度转换为度，反之亦然。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "cc7d5adb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.57079633 3.14159265 4.71238898 6.28318531]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([90, 180, 270, 360])\n",
    "\n",
    "x = np.deg2rad(arr)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8477f895",
   "metadata": {},
   "source": [
    "## 弧度转换为角度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "22703d6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 90. 180. 270. 360.]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([np.pi/2, np.pi, 1.5*np.pi, 2*np.pi])\n",
    "\n",
    "x = np.rad2deg(arr)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8661689",
   "metadata": {},
   "source": [
    "## 寻找角度\n",
    "从正弦、余弦、坦弦的值中寻找角度。例如，sin、cos和tan的逆运算（arcsin、arccos、arctan）。\n",
    "\n",
    "NumPy提供的ufuncs的 arcsin(), arccos()和arctan()可以为相应的sin, cos和tan值产生弧度值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2d9a6837",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.5707963267948966\n"
     ]
    }
   ],
   "source": [
    "x = np.arcsin(1.0)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6b6522cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.57079633 -1.57079633  0.10016742]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([1, -1, 0.1])\n",
    "\n",
    "x = np.arcsin(arr)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "416a3a7d",
   "metadata": {},
   "source": [
    "## 斜边\n",
    "在NumPy中使用勾股定理寻找斜边。\n",
    "\n",
    "NumPy提供了hypot()函数，根据勾股定理生成斜边。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "4077f017",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.0\n"
     ]
    }
   ],
   "source": [
    "base = 3\n",
    "perp = 4\n",
    "\n",
    "x = np.hypot(base, perp)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "565af08c",
   "metadata": {},
   "source": [
    "# Numpy 双曲函数\n",
    "## 双曲函数\n",
    "NumPy提供了sinh()、cosh()和tanh()三个函数，它们以弧度为单位取值并产生相应的sinh、cosh和tanh值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "da1ffe6e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.3012989023072947\n"
     ]
    }
   ],
   "source": [
    "x = np.sinh(np.pi/2)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "1cbc7f48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2.50917848 1.60028686 1.32460909 1.20397209]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([np.pi/2, np.pi/3, np.pi/4, np.pi/5])\n",
    "\n",
    "x = np.cosh(arr)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b58570f5",
   "metadata": {},
   "source": [
    "## 寻找角度\n",
    "从双曲正弦、余弦、正切的值中寻找角度。例如，sinh, cosh和tanh的逆运算（arcsinh, arccosh, arctanh）。\n",
    "\n",
    "Numpy提供的ufuncs中arcsinh(), arccosh()和arctanh()可以为给出的相应sinh, cosh和tanh值产生弧度值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "8c85e891",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.881373587019543\n"
     ]
    }
   ],
   "source": [
    "x = np.arcsinh(1.0)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "437255ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.10033535 0.20273255 0.54930614]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([0.1, 0.2, 0.5])\n",
    "\n",
    "x = np.arctanh(arr)\n",
    "\n",
    "print(x)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
